package com.laber.bbsServices.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.laber.bbsServices.dao.BbsUserMapper;
import com.laber.bbsServices.dao.BbsUserTokenMapper;
import com.laber.bbsServices.entity.BBSJSONModel;
import com.laber.bbsServices.entity.BbsUser;
import com.laber.bbsServices.entity.BbsUserToken;
import com.laber.bbsServices.utils.BBSUtis;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;

@Controller
public class LoginController {
	@Autowired
	private BbsUserMapper userDao;// 用户信息 数据库操作层
	@Autowired
	private BbsUserTokenMapper userTokenDao;// 用户token表操作
	private BBSJSONModel jsonMode;

	// 登陆验证的方法
	@RequestMapping(value = "login")
	@ResponseBody
	public Object login(HttpServletRequest request) {
		BbsUser user = new BbsUser();
		jsonMode = new BBSJSONModel();
		user.setUserName(request.getParameter("userName"));
		user.setPassWord(request.getParameter("passWord"));
		user = userDao.selectByUserNameAndPassWord(user);
		if (user != null) {
			BbsUserToken userToken = new BbsUserToken();
			userToken.setUserId(user.getId());
			userToken.setUserName(user.getUserName());
			userToken.setEndTime(BBSUtis.getStringDataNow());
			userToken.setToken(BBSUtis
					.getToken(new StringBuffer().append(user.getUserName()).append(userToken.getEndTime()).toString()));

			BbsUserToken selectToken = userTokenDao.selectByUserId(userToken);// 根据用户ID查询tonke信息
			int success = 0;
			if (selectToken != null) {// 如果有toke信息 更新token时间
				userToken.setId(selectToken.getId());
				success = userTokenDao.updateByPrimaryKey(userToken);// 更新
			} else {
				success = userTokenDao.insert(userToken);// 插入新的数据
			}
			if (success > 0) {
				jsonMode.code = 1;
				jsonMode.msg = "登录成功";
				jsonMode.data = userToken.getToken();
			} else {
				jsonMode.code = 0;
				jsonMode.msg = "用户名或者密码输入错误";
			}
		} else {
			jsonMode.code = 0;
			jsonMode.msg = "用户名或者密码输入错误";
		}
		return jsonMode;
		// HashMap<String, Object> userInfo = new HashMap<String, Object>();
		// userInfo.put("token", BBSGetToken.GetMD5Code(user.getUserName()));
		// userInfo.put("user", userDao.selectByUserNameAndPassWord(user));
		// jsonMode.data = userInfo;
		// if (jsonMode.data != null) {
		// jsonMode.code = 1;
		// jsonMode.msg = "成功";
		// }else {
		// jsonMode.code = 0;
		// jsonMode.msg = "用户名或者密码输入错误";
		// }
		// return jsonMode;
	}

	// 退出登陆
	@RequestMapping(value = "loginOut")
	@ResponseBody
	public Object loginOut(HttpServletRequest request) {
		jsonMode = new BBSJSONModel();
		BbsUserToken userToken = new BbsUserToken();
		userToken.setToken(request.getParameter("token"));	
		if (userTokenDao.deleteUserTokenByToekn(userToken) > 0) {
			jsonMode.code = 1;
			jsonMode.msg = "退出成功";
		} else {
			jsonMode.code = 0;
			jsonMode.msg = "退出失败";
		}
		return jsonMode;
	}

	//注册信息
	@RequestMapping(value="register")
	@ResponseBody
	public Object regiser(HttpServletRequest request) {
		jsonMode = new BBSJSONModel();
		BbsUser user = new BbsUser();
		user.setUserName(request.getParameter("userName"));
		user.setPassWord(request.getParameter("passWord"));
		user.setEmail(request.getParameter("email"));
//		user.setPhoneNumber(request.getParameter("phoneNumber"));
//		user.setSex(Integer.valueOf(request.getParameter("sex")));
		if(userDao.insert(user)>0){
			jsonMode.code = 1;
			jsonMode.msg = "注册成功";
			jsonMode.data = "success";
		} else {
			jsonMode.code = 0;
			jsonMode.msg = "注册失败";
		}
		
		return jsonMode;
	}
	
	@RequestMapping(value="getUserInfo")
	@ResponseBody
	public Object getUserInfo(HttpServletRequest request) {
		jsonMode = new BBSJSONModel();
		BbsUserToken userToken = new BbsUserToken();
		userToken.setToken(request.getParameter("token"));
		BbsUser user = userDao.selectByToken(userToken);
		if (user != null) {
			jsonMode.code = 1;
			jsonMode.msg = null;
			jsonMode.data = user;
		} else {
			jsonMode.code = 0;
			jsonMode.msg = null;
		}
		return jsonMode;
	}
	
	
	// 验证token是否过时
	public boolean isTokenOut(String tokenString) {
		BbsUserToken token = new BbsUserToken();
		token.setToken(tokenString);
		token = userTokenDao.selectByUserId(token);
		return compare_date(token.getEndTime());
	}

	/***
	 * 比较时间的先后
	 * 
	 * @param endDate 过期时间
	 *            
	 * @return true 已经过期 false没有过期
	 */
	public static boolean compare_date(String endDate) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date2 = df.format(new Date());
		try {
			Date dt1 = df.parse(endDate);
			Date dt2 = df.parse(date2);
			if (dt1.before(dt2)) {
				return true;
			} else {
				return false;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return false;
	}

}
